<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/31
 * Time: 20:08
 */
namespace app\reserve\controller;

use think\Controller;
use think\Request;
use app\reserve\model\Withdraw as WithdrawModel;
use app\reserve\model\Withdrawwash as WithdrawwashModel;
use think\Db;

class Withdraw extends Controller{

    /**
     * Notes:  添加提现
     * Create Date: 2018/10/31
     * Create by :lijie
     *
     * type 1 商户  2 洗车商户
     * washid   洗车商户id
     * money    提现金额
     * bank_card    银行卡号
     * openbank     开户银行
     * name         发起人姓名
     * phone        联系电话
     */
    public function addWithdraw(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"Withdraw.addWithdraw");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        // 开启事务
        Db::startTrans();
        // 模型判断
            $with       = new WithdrawModel();
            $table      = "cash";                   // 商户表名
            $id         = 'a_id';
        // 提现金额大于100
        if($param['money']<100){
            return apiResponse(201,"提现金额必须大于100",'');
        }
        // 判断提现金额足不足
        $money = Db::name($table)->where("$id",$param[$id])->value("wallet");
        if($param['money'] > $money){
            return apiResponse(201,"余额不足",'');
        }
//        // 每天提现次数控制
//        $start = strtotime(date("Y-m-d",time())." 00:00:00");
//        $end   = strtotime(date("Y-m-d",time())." 23:59:59");
//        $where['ctime'] = array("BETWEEN",array($start,$end));
//        $where["$id"]   = $param[$id];
//        $withCount = $with->where($where)->count();
//        if($withCount >= 3){
//            return apiResponse(201,"每日提现次数最多3次",'');
//        }
        // 添加提现记录
        $param['status'] = 0;
        $res = $with->addWith($param);
        // 商户金额减少
        $resCash = Db::name($table)->where("$id",$param[$id])->setDec('wallet',$param['money']);
        // 提交事务
        if($res && $resCash){
            Db::commit();
            return apiResponse(200,'提现成功',$res);
        }else {
            Db::rollback();
            return apiResponse(201,"提现失败",$res);
        }
    }

    /**
     * Notes:  编辑提现
     * Create Date: 2018/10/31
     * Create by :lijie
     *
     * id   提现列表id
     * type  1商户  2 洗车商户
     * status   状态 0 正在提现 1待确认 2已完成，3被驳回
     */
    public function editWithdraw(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"Withdraw.editWithdraw");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        // 模型判断
            $with = new WithdrawModel();
            $table      = "withdraw";                   // 商户表名
            $id         = 'a_id';
            $name       = "商户";
            $bill       = "cashbill";               // 账单表名
    
        //
        $where['id'] = $param['id'];
        $res = $with->editWith($param,$where);
        if($res){
            // if ($param['status'] == 1){
            //     // 打款时，生成账单
            //     $withdraw             = Db::name("{$table}")->where('id',$param['id'])->find();
            //     $data_bill['u_id']    = 0;
            //     $data_bill[$id]       = $withdraw[$id];
            //     $data_bill['price']   = $withdraw['money'];
            //     $data_bill['lessorad']= 1;
            //     $data_bill['content'] = $name."提现金额".$withdraw['money']."钱";
            //     $data_bill['ctime']   = time();
            //     $data_bill['utime']   = time();
            //     $data_bill['status']  = 0;
            //     Db::name("{$bill}")->insert($data_bill);
            // }
            return apiResponse(200,'编辑成功',$res);
        }else {
            return apiResponse(201,"编辑失败",$res);
        }
    }

    /**
     * Notes:  提现列表
     * Create Date: 2018/10/31
     * Create by :lijie
     */
    public function withdrawList(Request $request)
    {
        $param  = $request->post();
        $vali   = $this->validate($param,"Withdraw.withdrawList");
        if($vali !== true){
            return apiResponse(201,$vali,'');
        }
        $with = new WithdrawModel();
        $where = array();
        // 商户筛选
        if(!empty(input('post.a_id'))){
            $where['w.a_id'] = $param['a_id'];
        }
        if(!empty(input('post.washid'))){
            $where['w.washid'] = $param['washid'];
        }
        // 银行卡筛选
        if(!empty(input('post.bank_card'))){
            $where['w.bank_card'] = array("LIKE",'%'.$param['bank_card'].'%');
        }
        // 发起人筛选
        if(!empty(input('post.name'))){
            $where['w.name'] = array("LIKE",'%'.$param['name'].'%');
        }
        // 电话筛选
        if(!empty(input('post.phone'))){
            $where['w.phone'] = array("LIKE",'%'.$param['phone'].'%');
        }
        // 时间筛选
        if(!empty(input('post.start')) && !empty(input('post.end'))){
            $where['w.ctime'] = array("EGT",strtotime(input('post.start')));
            $where['w.ctime'] = array("ELT",strtotime(input('post.end')));
        }
        // 状态筛选
        if(!empty(input('post.status'))){
            $where['w.status'] = $param['status'];
        }
        $res['list'] = $with->selectWith($where,$param['pagenum'],$param['listNum']);
        $res['total'] = $with->alias("w")->where($where)->count();
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }

    /**
     * Notes:  提现详情
     * Create Date: 2018/10/31
     * Create by :lijie
     */
    public function withdrawInfo()
    {
        $id = input('post.id');
        if(empty($id)){
            return apiResponse(201,"id不能为空",'');
        }
        // 模型判断
        if(input('post.type') == 1){
            $with = new WithdrawModel();
        }else {
            $with = new WithdrawwashModel();
        }
        $where['id'] = $id;
        $res = $with->findWith($where);
        if($res){
            return apiResponse(200,'获取成功',$res);
        }else {
            return apiResponse(201,"获取失败",$res);
        }
    }
}